package com.cda.api.impl;

import com.cda.api.FaceApi;
import com.cda.entity.*;
import com.cda.service.DeviceinfoService;
import com.cda.service.SfFaceHeartBeatService;
import com.cda.service.SfdevicebindService;
import com.cda.utils.FaceHttpClientUtil;
import com.cda.utils.JwtHttpUtil;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.NameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Service("faceApi")
public class FaceApiImpl implements FaceApi  {


    @Value("${sf.http}")
    String sfHttp;

    @Autowired
    SfdevicebindService sfdevicebindService;
    @Autowired
    DeviceinfoService deviceinfoService;
    @Autowired
    SfFaceHeartBeatService sfFaceErrorLogService;
    /**
     * 获取设备绑定表参数
     * @return
     */
   public String[] getFaceIp(){
       InetAddress addr = null;
       String clientIp = "";
       String str [] = new String[2];
       try {
           addr = InetAddress.getLocalHost();
           clientIp = addr.getHostAddress().toString(); //获取本机ip


           SfdevicebindEntity sfb = sfdevicebindService.findByClientIp(clientIp);
           str[0] = sfb.getFaceIp();//人脸识别ip
           str[1] = sfb.getFacePort();
           return str;
       } catch (UnknownHostException e) {
           e.printStackTrace();
           return str;
       }
   }

    /**
     * 获取人脸设备key
     * @return
     */
    public String[] getFaceKey(String faceIp){

        String str [] = new String[2];
        DeviceinfoEntity di = deviceinfoService.findByIp(faceIp);
        System.out.println("【人脸设备表参数】："+JSONObject.fromObject(di));
        str[0] = di.getOldKey();
        str[1] = di.getNewKey();
        return str;
    }


    @Override
    public String setDeviceKey() {
        String result = null;
        try {
        String[]  str = getFaceIp();
        String url = "http://"+str[0]+":"+str[1]+"/setDeviceKey";
        String faceStr[] = getFaceKey(str[0]);
        JSONObject json  = new JSONObject();
        json.put("key",faceStr[0]);
        json.put("newKey",faceStr[1]);
             result = FaceHttpClientUtil.doPostForString(url,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }

    }

    @Override
    public String getVisitorConfig(String key) {
        String result = null;
        try {
        String[]  str = getFaceIp();
        String url = "http://"+str[0]+":"+str[1]+"/setDeviceKey";
        JSONObject param  = new JSONObject();
        String faceStr[] = getFaceKey(str[0]);
        param.put("key",faceStr[1] );

            result = FaceHttpClientUtil.doGetForString(url,param);

        } catch (Exception e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    @Override
    public String setDeviceInfo(JSONObject content) {
        String result = null;
        try {
        String[]  str = getFaceIp();
        String url = "http://"+str[0]+":"+str[1]+"/setDeviceInfo";
            String faceStr[] = getFaceKey(str[0]);
            content.put("key",faceStr[1]);
            result = FaceHttpClientUtil.doPostForString(url,content);
        } catch (Exception e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }

    }

    @Override
    public String getDeviceInfo() {

        String result = null;
        try {
        String[]  str = getFaceIp();
        String url = "http://"+str[0]+":"+str[1]+"/getDeviceInfo";
        JSONObject param  = new JSONObject();
        String faceStr[] = getFaceKey(str[0]);
        param.put("key",faceStr[1]);
            result = FaceHttpClientUtil.doGetForString(url,param);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    @Override
    public String setPerson(JSONObject content) {
        String result = null;
        try {
        String[]  str = getFaceIp();
        String url = "http://"+str[0]+":"+str[1]+"/setPerson";
            String faceStr[] = getFaceKey(str[0]);
            content.put("key",faceStr[1]);
            result = FaceHttpClientUtil.doPostForString(url,content);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }

    }

    @Override
    public String removePerson(String s) {
        String result = null;
        try {
            String[]  str = getFaceIp();
            String url = "http://"+str[0]+":"+str[1]+"/removePerson";
            NameValuePair[] data = new NameValuePair[2] ;
            String faceStr[] = getFaceKey(str[0]);

            data[0] = new NameValuePair("key",faceStr[1]);
            data[1] = new NameValuePair("id","["+s+"]");

            result = FaceHttpClientUtil.doPostForString2(url,data);
            System.out.println("removePerson:"+result);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    @Override
    public String setRecordCallback(String url) {
        String result = null;
        try {
        String[]  str = getFaceIp();
        String httpurl = "http://"+str[0]+":"+str[1]+"/setRecordCallback";

            JSONObject json = new JSONObject();
            String faceStr[] = getFaceKey(str[0]);
            json.put("key",faceStr[1]);
            json.put("url",url);
            result = FaceHttpClientUtil.doPostForString(httpurl,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    @Override
    public String getRecordCallback() {
        String result = null;
        try {
        String[]  str = getFaceIp();
        String httpurl = "http://"+str[0]+":"+str[1]+"/getRecordCallback";

            JSONObject json = new JSONObject();
            String faceStr[] = getFaceKey(str[0]);
            json.put("key",faceStr[1]);
            result = FaceHttpClientUtil.doGetForString(httpurl,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    @Override
    public String setHeartBeat(String url, double period) {
        String result = null;
        try {
        String[]  str = getFaceIp();
        String httpurl = "http://"+str[0]+":"+str[1]+"/setHeartBeat";

            JSONObject json = new JSONObject();
            String faceStr[] = getFaceKey(str[0]);
            json.put("key",faceStr[1]);
            json.put("url",url);
            json.put("period",period);
            result = FaceHttpClientUtil.doPostForString(httpurl,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    @Override
    public String check(String photo) {
        String result = null;
        try {
            String[]  str = getFaceIp();
            String httpurl = "http://"+str[0]+":"+str[1]+"/check";

            JSONObject json = new JSONObject();
            String faceStr[] = getFaceKey(str[0]);
            json.put("key",faceStr[1]);
            json.put("photo",photo);
            result = FaceHttpClientUtil.doPostForString(httpurl,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    @Override
    public String photo(String tipsBefore, String tipsAfter, int count) {
        String result = null;
        try {
            String[]  str = getFaceIp();
            String httpurl = "http://"+str[0]+":"+str[1]+"/photo";

            JSONObject json = new JSONObject();
            String faceStr[] = getFaceKey(str[0]);
            json.put("key",faceStr[1]);
            json.put("tipsBefore",tipsBefore);
            json.put("tipsAfter",tipsAfter);
            json.put("count",count);
            result = FaceHttpClientUtil.doPostForString(httpurl,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
            return result;
        }

       return result;
    }

    @Override
    public String removeRecord(double ts) {
        String result = null;
        try {
        String[]  str = getFaceIp();
        String httpurl = "http://"+str[0]+":"+str[1]+"/removeRecord";

            JSONObject json = new JSONObject();
            String faceStr[] = getFaceKey(str[0]);
            json.put("key",faceStr[1]);
            json.put("ts",ts);
            result = FaceHttpClientUtil.doPostForString(httpurl,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }


    @Override
    public String getHeartBeat() {
        String result = null;
        try {
            String[]  str = getFaceIp();
            String httpurl = "http://"+str[0]+":"+str[1]+"/getHeartBeat";

            JSONObject json = new JSONObject();
            String faceStr[] = getFaceKey(str[0]);
            json.put("key",faceStr[1]);
            result = FaceHttpClientUtil.doPostForString(httpurl,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
        }finally {
            return result;
        }
    }


    /**
     * 上传人脸识别设备图像到服务端
     * @param p
     */
    public void uploadRecord(Person p) {

        System.out.println("上传人脸识别设备图像到服务端");
        try {
            String[]  str = getFaceIp();
            String url = sfHttp+"/rest/sfFacePhotoController/doAddApi";
            SfFacePhotoEntity sf = new SfFacePhotoEntity();
            sf.setId(p.getId());
            sf.setIcNo(p.getIC_NO());
            sf.setPhoto(p.getPhoto());
            sf.setIdNo(p.getID_NO());
            sf.setCreateName(str[0]);//需要剔除的人脸识别分发的ip，暂时存储在这个属性中

            com.alibaba.fastjson.JSONObject result=JwtHttpUtil.httpRequest( url, "POST", JSONObject.fromObject(sf).toString(),getToken() );
            System.out.println(" 上传人脸识别设备图像到服务端返回结果："+result.toString());
        } catch (Exception e) {
            System.out.println(" 上传人脸识别设备图像到服务端失败："+e.getMessage());
            e.printStackTrace();
        }

    }

    @Override
    public String open() {

        String result = null;
        try {
            String[]  str = getFaceIp();
            String httpurl = "http://"+str[0]+":"+str[1]+"/open";
            JSONObject json = new JSONObject();
            String faceStr[] = getFaceKey(str[0]);
            json.put("key",faceStr[1]);
            result = FaceHttpClientUtil.doPostForString(httpurl,json);
        } catch (IOException e) {
            result = e.getMessage();
            e.printStackTrace();
            return result;
        }
        return result;

    }

    //获取token
    public  String getToken(){
        String url = sfHttp+"/rest/tokens?username=interfaceuser&password=123456";
        String token= JwtHttpUtil.httpRequest(url, "POST", null);
        return token;
    }



}
